package zuk.clipvietfun;

import java.io.File;

import zuk.clipvietfun.plus.CheckConnection;
import zuk.clipvietfun.plus.DirAndFile;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class AppStart extends FragmentActivity {
	Context context = this;
	Intent i;
	DirAndFile dirNfile = new DirAndFile();	
	File dirHistory,fileHistory,dir;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appstart);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}

		dir = new File(getBaseContext().getCacheDir() + "/data");
		DirAndFile.deleteDir(dir);
		
		ProgressBar pgBar = (ProgressBar)findViewById(R.id.pgBar_loadscreen);
		
		TextView text = (TextView)findViewById(R.id.load_text);
		
		CheckConnection cn = new CheckConnection();
		if(!cn.isOnline(context)){
			Toast.makeText(context, "No internet connection!!!", Toast.LENGTH_LONG).show();
			text.setText("There's no network connection right now. Try again later.");
		}else {
			pgBar.setMax(7);
			new LoadScreen(dir,pgBar,context,starMain).execute();
			i = new Intent(context, Main.class);
		}
		

	}
	private Handler starMain =new Handler(new Callback() {
		public boolean handleMessage(Message msg) {
			startActivity(i);
			finish();
			return true;
		}
	});

	public Context getAppStartContext(){
		return context;
	}
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

}
